<template>
	<view class="ag_container">
		<p class="per_title">
			<span><i class="iconfont">&#xe66e;</i>添加/删除抄送人</span>
			<button size="mini" class="primay_btn" @click="copyConfirm">确 定</button>
		</p>
		<uni-table ref="table" border stripe emptyText="暂无更多数据" class="copy-table">
				<!-- 个人 -->
				<uni-tr  v-for="item,index in contractCarbonCopyList" :key="index">
					<uni-td v-if="item.senderType == 1">
						<uni-easyinput v-model="item.sendName" placeholder="抄送人真实姓名" prefixIcon="person" suffixIcon="forward" @iconClick="personClick(index)" />
						<uni-easyinput v-model="item.sendMobile" placeholder="抄送人实名手机号" />
					</uni-td>
					<!-- 企业 -->
					<uni-td v-if="item.senderType == 0">
						<uni-easyinput v-model="item.sendCompany" placeholder="营业执照的企业名称" prefixIcon="wallet" suffixIcon="forward" @iconClick="companyNameClick(index)" />
					    <uni-easyinput v-model="item.sendName" placeholder="抄送人真实姓名" prefixIcon="person" suffixIcon="forward" @iconClick="companyStaffClick(index)" />
						<uni-easyinput v-model="item.sendMobile" placeholder="抄送人实名手机号" />
					</uni-td>
					<uni-td><i class="iconfont" @click="removeCopy(index, contractCarbonCopyList, item)">&#xe8c1;</i></uni-td>
				</uni-tr>
		</uni-table>			
					
		<view class="copy-block">
			<button size="mini" @click="addWrite(0)"><i class="iconfont">&#xe647;</i>添加个人抄送</button>
			<button size="mini" @click="addWrite(1)"><i class="iconfont">&#xe647;</i>添加企业抄送</button>
		</view>
	</view>
</template>

<script>
	import {listContractSignCopyByContractId} from "@/api/ec/work/contract/contractSign";
	import {getCustomerCompany} from "@/api/ec/work/customer/customerCompany";
	import {getCustomerPerson} from "@/api/ec/work/customer/customerPerson";
	import {getCustomerCompanyStaff} from "@/api/ec/work/customer/customerCompanyStaff";
	import {delContractCopy,addContractCopyMore} from "@/api/ec/work/contract/contract";
	export default {
		components: {},
		data() {
			return {
				customerCompanyId:'', //当前选中的 常用企业id
				contractCarbonCopyList:[], //抄送数据
				setSizeCopy:0, //抄送个数
				contractId:'', //合同id
				contractFlow:null, //合同信息
				customerCompanyId:null,    
			};
		},
		onLoad(option) {
			this.contractFlow = JSON.parse(option.data)
			this.contractId = this.contractFlow.contractId
			this.getList(this.contractId)
			this.bindData(this.contractId)
		},
		methods: {
			bindData(){
				uni.$on('copyStaffData',data=>{
					this.contractCarbonCopyList[data.index].contractId = Number.parseInt(this.contractId);
					this.contractCarbonCopyList[data.index].sendPersonId = data.sendPersonId
					this.contractCarbonCopyList[data.index].sendName = data.sendName
					this.contractCarbonCopyList[data.index].sendMobile = data.sendMobile
				})
				uni.$on('copyCompanyData',data=>{ 
					this.contractCarbonCopyList[data.index].contractId = Number.parseInt(this.contractId);
					this.contractCarbonCopyList[data.index].sendName = ''
					this.contractCarbonCopyList[data.index].sendMobile = ''
					this.contractCarbonCopyList[data.index].sendCompany = data.sendCompany
					this.customerCompanyId = data.customerCompanyId
				})
				uni.$on('copyCompanyStaffData',data=>{
					this.contractCarbonCopyList[data.index].contractId = Number.parseInt(this.contractId);
					this.contractCarbonCopyList[data.index].sendCompany = data.sendCompany
					this.contractCarbonCopyList[data.index].sendName = data.sendName
					this.contractCarbonCopyList[data.index].sendMobile = data.sendMobile	
					this.contractCarbonCopyList[data.index].sendCompanyId = this.customerCompanyId;
				})
			},
			// 确定
			copyConfirm() {
			  // 检验抄送人信息重复
			  let arr = this.contractCarbonCopyList;
			  for (let i = 0; i < arr.length; i++) {
			    for (let j = i + 1; j < arr.length; j++) {
			      if (arr[i].sendMobile == arr[j].sendMobile) {
			        this.$modal.msgError("抄送人信息已存在，保存失败！");
			        return false;
			      }
			    }
			  };
			  let flag = this.contractCarbonCopyList.every(item => !!item.sendName)
			  if (!flag) {
			    this.$modal.msgError('添加内容不能为空！')
			    return;
			  } else {
			    //不为空执行的代码
			    this.contractCarbonCopyList.forEach(e => {
			      if (this.contractFlow.status == this.$enums.contract_sign_flow_status.ALL_OK) {
			        e.status = 1;
			        // 签署完成时间
			        e.updateTime = this.contractFlow.updateTime;
			      }
			      e.contractId = this.contractFlow.contractId;
			      e.contractNumber =  this.contractFlow.contractNumber;
			      e.contractName =  this.contractFlow.contractName;
			    });
				// 多条抄送 array
				addContractCopyMore(this.contractCarbonCopyList).then((response) =>{
				  this.$modal.msgSuccess("添加/删除抄送人成功！");
				  setTimeout(()=>{
					  uni.switchTab({
					  	url:'/pages/index'
					  })
				  },300)
				})      
			  }
			},    
			// 个人选择
			personClick(index){
				uni.navigateTo({
					url:'/pages/sign/select/selectPersonal?personalCopy=' + true+'&index='+index
				})
			},
			// 企业名字选择
			companyNameClick(index){
				uni.navigateTo({
					url:'/pages/sign/select/selectCompany?companyCopy=' + true+'&index='+index
				})
			},
			// 企业选择
			companyStaffClick(index){
				let customerCompanyId = this.customerCompanyId?this.customerCompanyId:''
				uni.navigateTo({
					url:'/pages/sign/select/selectCompanyPersonal?companyCopy=' + true + '&customerCompanyId='+ customerCompanyId+'&index='+index
				}) 
			},
			// 删除抄送数据
			removeCopy(index, rows, row){
			  rows.splice(index, 1);
			  if(!!row.id){
			    delContractCopy(row.id);
			    this.$modal.msgSuccess("删除成功！");
			  }
			},    
			// 抄送添加
			addWrite(value) {
			  if (value == 0) {
			    this.contractCarbonCopyList.push({
			      sendMobile: "",
			      sendName: "",
			      senderType: 1, // 1 个人抄送
			      sendPersonId:'',
			      contractId:'',
			    });
			  } else {
			    this.contractCarbonCopyList.push({
			      sendCompany: "",
			      sendMobile: "",
			      sendName: "",
			      senderType: 0,  // 0 企业抄送
			      sendCompanyId:'',
			      contractId:'',
			    });
			  }
			},    
			getList(contractId){
				this.$modal.loading();
				listContractSignCopyByContractId(contractId).then((response)=>{
					uni.hideLoading()
					this.contractCarbonCopyList = response.data
				})
			},

		},
	};
</script>

<style lang="scss" scoped>
	.table_view{
		height: 100rpx;
		line-height: 100rpx;
		border-bottom: 1rpx solid #ebeef5;
		padding: 0 20rpx 0 0;
		display: flex;
		justify-content: space-between;
		margin:0 20rpx;
	}
	.uni-easyinput{
		margin: 18rpx 0 20rpx;
	}
	.copy-table{
		margin: 40rpx 0 60rpx;
	}
	.copy-block{
		display: flex;
		justify-content: space-between;
		width: 85%;
		margin: 0 auto;
		.iconfont{
			color: #999;
			font-size: 26rpx;
			margin-right: 4rpx;
		}
		button{
			background: #fff;
			border:1rpx solid #ccc;
		}
	}
	page {
		background: $uni-bg-color;
	}
	.per_title {
		font-size: 28rpx;
		color: #666666;
		border-bottom: 1rpx solid #d3d3d3;
		padding-bottom: 12rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.iconfont {
			padding-right: 6rpx;
			font-size: 30rpx;
		}
	}

	.ag_container {
		position: relative;
		padding: 30rpx 40rpx 0;
	}
</style>